﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;
using EFong.View.Proxy;
using EFong.Contract;

namespace EFong.View.Financing
{
    public partial class Frm_VendorSettlement : BasicMdiChildForm
    {
        private Proxy.FinanceServiceProxy _financeServiceProxy { get { return Proxy.FinanceServiceProxy.GetInstance(); } }
        private Proxy.MasterServiceProxy _masterProxy {get{ return Proxy.MasterServiceProxy.GetInstance();}}
        private Proxy.BaseServiceProxy _baseServiceProxy { get { return Proxy.BaseServiceProxy.GetInstance(); } }

        private List<VendorSettlement> _vendorSettlements;

        public Frm_VendorSettlement()
        {
            InitializeComponent();
        }

        protected override void InitFields()
        {
            _vendorSettlements = new List<VendorSettlement>();
        }

        protected override void LoadDatas()
        {
            this.repositoryItemComboBox2.BindingData(_masterProxy.GetAllEffectivedBankAccount());
        }

        protected override void Events()
        {
            this.btn_Search.Click += (s, e) =>
            {
                DateTime startDate = dte_StartDate.GetValue(new DateTime(2000, 1, 1));
                DateTime endDate = dte_EndDate.GetValue(new DateTime(3000, 1, 1));

                var result = _financeServiceProxy.GetVendorSettlementByDate(startDate, endDate);
                if (result.IsSuccess) 
                {
                    this.grd_Orders.DataSource = result.ReturnValue;
                }
            };

            this.gv_Orders.ShowingEditor += (s, e) =>
                {
                    VendorSettlement vs = this.gv_Orders.GetRow(this.gv_Orders.FocusedRowHandle) as VendorSettlement;

                    if (this.gv_Orders.FocusedColumn == gv_Orders.Columns["PaymentType"])
                    {
                        if (!string.IsNullOrEmpty(vs.PaymentType) && vs.PaymentType != "现金")
                            e.Cancel = true;
                        else
                            e.Cancel = false;
                    }
                };

            this.gv_Orders.CellValueChanged += (s, e) =>
                {
                    VendorSettlement vs = this.gv_Orders.GetRow(this.gv_Orders.FocusedRowHandle) as VendorSettlement;

                    _vendorSettlements.Add(vs);

                    if (vs == null)
                        return;

                    if (e.Column == gv_Orders.Columns["PaymentType"])
                    {
                        vs.BankAccount = null;

                        this.gv_Orders.BeginDataUpdate();
                        this.gv_Orders.EndDataUpdate();
                    }
                };
        }

        private void bar_Main_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                case "bbt_Save":
                    this.save();
                    break;
                case "bbt_Close":
                    this.Close();
                    break;
                default:
                    break;
            }
        }

        private void save()
        {
            this.gv_Orders.PostEditor();

            UIOperManager.SaveData(
                () =>
                {
                    return true;
                },
                () =>
                {

                    return _financeServiceProxy.UpdateVendorSettlementList(_vendorSettlements).IsSuccess;
                },
                "保存成功.",
                () =>
                {
                    this.Close();
                });
        }

    }
}
